//查询演练室的所有事件

var getevents = {
	getevents_list : function() {
		console.info(getclassrooms.selectedContent);
		$("#ecdName").val(getclassrooms.selectedContent.ecdClassRoomName);
		$("#protName").val(getclassrooms.selectedContent.projectName);
		$("#ecdDescription").val(getclassrooms.selectedContent.ecdClassRoomDescription);
		var userJson = JSON.parse(sessionStorage.getItem("suid")).data;
		$("#login_name2").html(userJson.su_name);
		var crid = sessionStorage.getItem("crid");
		$.ajax({
					type : "GET",
					url : "http://127.0.0.1:8080/yl-websocket/transmit?param='m=getevents,crid="+ crid +"&address=http://103.37.166.4:7777/StateGrid/cra.do",
					async : false,
					scriptCharset : 'utf-8',
					success : function(param) {
						param = JSON.parse(param).data;
//						getevents.userList(param)
//						getevents.userllert(param);
						getevents.createTable(param);
//						alert(param);
					},
					error : function() {
					}
				});
	},
	//获取用户
	userList : function(){
		var eventid = sessionStorage.getItem("ownerClassRoomEventId");
		$.ajax({
			type : "GET",
			url : "http://127.0.0.1:8080/yl-websocket/transmit?param='m=eventusers,eventid="+ eventid +"&address=http://103.37.166.4:7777/StateGrid/ua.do",
			async : false,
			scriptCharset : 'utf-8',
			success : function(param) {
//				alert(param);
				param = JSON.parse(param).data;
				getevents.getroleques(param);
			},
			error : function() {
			}
		});
	},
	//研讨情况
	getroleques : function(param){
		$("[name='cb']").attr("checked",false);
		var ecdrepName = sessionStorage.getItem("ecdrepName");
		var suid = "";
		var su_name = "";
		var pid = sessionStorage.getItem("ecdrepId");
		for(var i in param){
			var ecdrepId = param[i].ecdrepId
			if(ecdrepId == pid){
				 suid = param[i].realizeUsers[0].su_id;
				 su_name = param[i].realizeUsers[0].su_name;
				 $("[name='cb'][value='"+suid+"']").prop("checked",true);
			}
		}
		$("#Js_name").html(ecdrepName);
		$("#Yh_name").val(su_name);
		
	},
	userllert : function(param){
		var eventid=""
			for(var i in param){
				eventid = param[i].ceventId
				$.ajax({
					type : "GET",
					url : "http://127.0.0.1:8080/yl-websocket/transmit?param='m=eventusers,eventid="+ eventid +"&address=http://103.37.166.4:7777/StateGrid/ua.do",
					async : false,
					scriptCharset : 'utf-8',
					success : function(param) {
//						alert(param);
						sessionStorage.setItem("parames", param);
						parames = JSON.parse(param).data;
						getevents.createTable()
					},
					error : function() {
					}
				});
			}
		
	},
	getUsers : function(eventid){
		$.ajax({
			type : "GET",
			url : "http://127.0.0.1:8080/yl-websocket/transmit?param='m=eventusers,eventid="+ eventid +"&address=http://103.37.166.4:7777/StateGrid/ua.do",
			async : false,
			scriptCharset : 'utf-8',
			success : function(param) {
//				alert(param);
//				sessionStorage.setItem("parames", param);
				getevents.allUsers = JSON.parse(param).data;
//				getevents.createTable()
			},
			error : function() {
			}
		});
	},
	allUsers : {},
	createTable : function(params) {
		$("#getevents_list").html("");
		var str = "";
		var j = 0;
		var n = 0;
		for ( var i in params) {
			
			j++;
			str += '<div style="max-height: 162px;overflow-y: scroll;"><table class="Ptable" id="">'
			var s1 = "#a0a0ff";
			var s2 = "#ff9900";
			var s3 = "#ff3300";
			var s4 = "#0070c0";
			var s5 = "#ff00cc";
			var colors = [ {
				"color" : "#a0a0ff"
			}, {
				"color" : "#ff9900"
			}, {
				"color" : "#ff3300"
			}, {
				"color" : "#0070c0"
			}, {
				"color" : "#ff00cc"
			}, ]

			j = 0;
			
			getevents.getUsers(params[i].ceventId); //fenbie
			
			for ( var k in params[i].ecdClassRoomEventPostList) {
				j++;
				n--
				var h = params[i].ecdClassRoomEventPostList.length + 1;
				var userName;
				for(var ii in getevents.allUsers){
					if(params[i].ecdClassRoomEventPostList[k].ecdrepId == getevents.allUsers[ii].ecdrepId){
						userName = getevents.allUsers[ii].realizeUsers[0].su_name;
					}
				}
				if (k == 0) {
					str += '<tr><td rowspan="'
							+ h
							+ '" style="background:'+colors[i].color+';width: 1px;" class="'
							+ 's'
							+ j
							+ '"></td><td colspan="3" rowspan="'
							+ h
							+ '" style="width: 40px;" valign="top">'
							+ params[i].ceventName
							+ '</td><td colspan="3">【角色'
							+ j
							+ '】<span class="geren"></span>'
							+ params[i].ecdClassRoomEventPostList[k].ecdrepName
							+ '</td><td colspan="2">【用户】'
							+ userName
							+ '</td><td><a href="javascript:;" class="addUser" id="editUser" onclick="editUser(\''+ params[i].ecdClassRoomEventPostList[k].ownerClassRoomEventId + '\',\''+ params[i].ecdClassRoomEventPostList[k].ecdrepId + '\',\''+ params[i].ecdClassRoomEventPostList[k].ecdrepName + '\')"></a></td></tr>';
				} else {
					str += '<tr><td colspan="3">【角色'
							+ j
							+ '】<span class="geren"></span>'
							+ params[i].ecdClassRoomEventPostList[k].ecdrepName
							+ '</td><td colspan="2">【用户】'
							+ userName
							+ '</td><td><a href="javascript:;" class="addUser" id="editUser" onclick="editUser(\''+ params[i].ecdClassRoomEventPostList[k].ownerClassRoomEventId + '\',\''+ params[i].ecdClassRoomEventPostList[k].ecdrepId + '\',\''+ params[i].ecdClassRoomEventPostList[k].ecdrepName + '\')"></a></td></tr>';
				}
			}
			str += '</table></div>'
			console.info(params);

		}
		$(str).appendTo("#getevents_list");
		$("#getevents_list1").html(str);
		// $(str).appendTo("#getevents_pros")
	},

}



var baocun ={
		
		baocun_add : function(){
//			getclassrooms.getclassrooms_list();
			var crid = sessionStorage.getItem("crid");
			var name = $("#ecdName").val();
			var desc = $("#ecdDescription").val();
			var alive = $("#alive option:selected").val();
			baocun.getAlive = alive;
			if(alive==0){
				alive = "0";//未用
			}else if(alive==1){
				alive = "1";//在用
			}else if(alive==2){
				alive = "2"//已用
			}
			
			$.ajax({
				type : "GET",
				url : "http://127.0.0.1:8080/yl-websocket/transmit?param='m=updcr,alive="+ alive +",name="+ name +",desc="+ desc +",crid="+ crid +"&address=http://103.37.166.4:7777/StateGrid/cra.do",
				async : false,
//				 data : ({'name':name,'alive':alive,'crid':crid,'desc':desc}),
				scriptCharset : 'utf-8',
				success : function(param) {
					 location.reload();
				},
				error : function() {
				}
			});
			
		},
		getAlive : ""
	}

var getMaterbj = {
		getMaterbj_list : function(){
			$.ajax({
				type : "GET",
				url : "http://127.0.0.1:8080/yl-websocket/transmit?param='m=getmater,type=refdoc,suid=editor1&address=http://103.37.166.4:7777/StateGrid/mat.do",
				scriptCharset: 'utf-8', 
				success : function(param) {
//					alert(param);
					param = JSON.parse(param).data;
					getMaterbj.getMaterbj_xs(param)
				},
				error: function(){
				}
			});
		},
		getMaterbj_xs : function(param){
			$("#wenDangbj").html("");
			var str = ""
			var j = 0;
//				str += '<tr><th><input type="checkbox"></th><th>素材编号</th><th>素材名称</th><th>操作</th></tr>';
				str += '<tr><td><input type="checkbox" name="" id="" value="" /></td><td>素材编号</td><td>素材名称 </td><td>是否被引用</td><td>被引用对象</td></tr>';
			for(var i in param){
				j++;
				str += '<tr><td><input name="cb" onclick="sigleChecked(this)"   value="'+param[i].materInfoId+'" type="checkbox"/></td><td>'+j+'</td><td>'+param[i].materName+'</td><td>是</td><td>'+param[i].materFileName+'</td></tr>';
			}
			$(str).appendTo("#wenDangbj");
			
		} 
}
//$('input[name="cb"]:checked').each(function(){    
//	   chk_value.push($(this).val());    
//	  });    
function sigleChecked(u){
   if ($(u).prop('checked')) {
	   $(':checkbox[name=cb]').removeAttr('checked');
	   $(u).prop('checked',true);
   }
}
//function a(){
//	var a = $('input[name="cb"]:checked').val();
//	console.info(a);
//}
//新增文档
var addgetMaterbj = {
		addgetMaterbj_list : function(){
			var materid = $('input[name="cb"]:checked').val();
//			console.info(materid);//控制台打印
			var crid = sessionStorage.getItem("crid");
			$.ajax({
				type : "GET",
				url : "http://127.0.0.1:8080/yl-websocket/transmit?param='m=addmc,crid="+ crid +",materid="+ materid +",desc=4test&address=http://103.37.166.4:7777/StateGrid/mat.do",
				scriptCharset: 'utf-8', 
				success : function(param) {
//					alert(param);
					param = JSON.parse(param)
					addgetMaterbj.addgetMaterbj_xs(param)
					$(".adddocPop").hide(500);
					$(this).removeClass("overHiden");
				},
				error: function(){
				}
			});
		},
		addgetMaterbj_xs : function(param){
			var a = param.materInfo.materName
			$("#addWenDangbj").html("");
			var str = "";
			var j=0 ;
				j++;
				str +='<tr><td><input type="checkbox" name="" id="" value="" /></td><td>'+j+'</td><td>'+param.materInfo.materName+'</td><td><a onclick="addgetMaterbj.deleteWd(\''+param.crjmId+'\')">删除</a></td></tr>';
			$("#addwenDangbj").append(str);
		},
		deleteWd :function(crjmid){
			alert(crjmid);
			$.ajax({
				type : "GET",
				url : "http://127.0.0.1:8080/yl-websocket/transmit?param='m=delmc,crjmid="+ crjmid +"&address=http://103.37.166.4:7777/StateGrid/mat.do",
				scriptCharset: 'utf-8', 
				success : function(param) {
					alert(param);
					$("#"+crjmi).empty();
					param = JSON.parse(param)
				},
				error: function(){
				}
			});
		}
}
//查询所用用户
var userAll = {
		userAll_list : function(){
			$.ajax({
				type : "GET",
				url : "http://127.0.0.1:8080/yl-websocket/transmit?param='m=allusers&address=http://103.37.166.4:7777/StateGrid/ua.do",
				scriptCharset: 'utf-8', 
				success : function(param) {
//					alert(param);
					param = JSON.parse(param);
					 userAll.userAll_show(param.data);
				},
				error: function(){
				}
			});
		},
		userAll_show : function(param){
			$("#allUser").html("");
			var str = "";
//			var j = 0;
			for(var i in param){
//				j++ 
				str += '<label><input name="cb" onclick="siglChecked(this)"   value="'+param[i].su_id+'" type="checkbox">'+param[i].su_name+'</input></label>';
				
			}
			$(str).appendTo("#allUser");
			$("#Yh_name").val($('input[name="cb"]:checked').val());
		}
}

function siglChecked(u){
	   if ($(u).prop('checked')) {
		   $(':checkbox[name=cb]').removeAttr('checked');
		   $(u).prop('checked',true);
		   var wwww=$(u).parent().text();
//		   alert(wwww);
		   $("#Yh_name").val(wwww);
		  
			   
		   
	   }
	}

//保存编辑事件
var baoCunsj = {
		baoCun_sj : function() {
			var su_id = $('input[name="cb"]:checked').val()
			var crid = sessionStorage.getItem("crid");
			var eventid = sessionStorage.getItem("ecdrepId");
			var idval = eventid+';'+su_id+';;';
			$.ajax({
				type : "GET",
				url : "http://127.0.0.1:8080/yl-websocket/transmit?param='m=binding,crid="+ crid +",idval="+ idval +"&address=http://103.37.166.4:7777/StateGrid/cru.do",
				scriptCharset: 'utf-8', 
				success : function(param) {
//					alert(param);
					getevents.getevents_list();
					$(".editUserPop").hide(500);
					$(this).removeClass("overHiden");
					
				},
				error: function(){
				}
			});
			
		}
}


$(function() {
	userAll.userAll_list();
	
	$("#baoCunbj").click(function(){
		baoCunsj.baoCun_sj();
	})
		
	
	$("#baocun").click(function(){
		baocun.baocun_add();
	});
	//编辑
	$(this).addClass("overHiden");
	$("#edit").click(function() {
		$(".editPop").show(500);
		$(this).addClass("overHiden");
		getevents.getevents_list();
	
	});
	/*新增----添加文档*/
	$("#addDoc").click(function() {
		//alert("ok")
		$(".adddocPop").show(500);
		$(this).addClass("overHiden");
		getMaterbj.getMaterbj_list();
	});
	
	//保存文档
	$("#bcWendangbj").click(function(){
		addgetMaterbj.addgetMaterbj_list();
	})


});
function editUser(ownerClassRoomEventId,ecdrepId,ecdrepName){
		$(".editUserPop").show(500);
		sessionStorage.setItem("ownerClassRoomEventId", ownerClassRoomEventId);
		sessionStorage.setItem("ecdrepId", ecdrepId);
		sessionStorage.setItem("ecdrepName", ecdrepName);
		getevents.userList();
		
		
}



